<?php
/*
	产品分类相关控制器
*/
namespace hotel\modules\apiadmin\controllers;
use hotel\common\models\App;
use hotel\common\models\XopConfig;


use hotel\common\utils\Tool;
use \Yii;
use yii\base\Exception;

class AppController extends CoreController
{
	public function actionAppList()
	{
		$params = array(
			'limit' => $this->request('page_size',10),
			'page'	=> $this->request('page',1),
		);
		$whereArr = $where = $and = [];
		$and[] = ['in','appid',$this->permissionApps];
		$whereArr['where'] = $where;
		$whereArr['and'] = $and;

		if($this->userInfo['issuper'] == 1){
			$list = App::AppList([],$params);
		}else{
			$list = App::AppList($whereArr,$params);
		}
		$pages = App::$pages;
		$data['list'] = $list;
		$data['pageInfo'] = [
			'count' => $pages,
			'pageSize' => $params['limit'],
			'currPage' => $params['page'],
			'tatalPage' => ceil($pages/$params['limit']),
		];
		return Tool::showRes([0, '实例列表'],$data);
	}

	//根据appid获取项目数据
	public function actionGetAppDataById(){
		$appid = $this->request('appid');
		$app_data = App::find()->where("appid = '$appid'")->asArray()->one();
		$xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
		$app_data['xop_config_data'] = $xop_config_data;
		return Tool::showRes([0, '实例数据'],$app_data);
	}

	/*
    修改
*/
	public function actionAppEdit()
	{
		try {
			$app_id = $this->request('app_id');
			$model = new App();
			$model = $model::findOne($app_id);
			$model->pay_channel_mer_id = $this->request('pay_channel_mer_id');
			$model->pay_app_id = $this->request('pay_app_id');
			$model->pay_channel_id = $this->request('pay_channel_id');
			$model->pay_aid = $this->request('pay_aid');
			$model->pay_key = $this->request('pay_key');
			$model->comid = $this->request('comid');
			$model->coupon_url = $this->request('coupon_url');
			$model->coupon_channel = $this->request('coupon_channel');
			$model->coupon_key = $this->request('coupon_key');
			$model->is_place_order = $this->request('is_place_order');

            $model->ali_pay_channel_mer_id = $this->request('ali_pay_channel_mer_id');
            $model->ali_pay_app_id = $this->request('ali_pay_app_id');
            $model->ali_pay_aid = $this->request('ali_pay_aid');
            $model->ali_pay_key = $this->request('ali_pay_key');
			if ($model->save()) {
				return Tool::showRes([0, '操作成功']);
			}
			return Tool::showRes([400, '操作失败']);
		} catch (Exception $e) {
			echo $e->getMessage();
			// die(); // 终止异常
		}
	}

	//西软参数修改
	public function actionEditXopConfig()
	{
		try {
			$appid = $this->request('appid');
			$res = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
			if($res){
				$model = XopConfig::findOne($res['id']);
			}else{
				$model = new XopConfig();
			}
			$model->appid = $appid;
			$model->appkey = $this->request('appkey');
			$model->secret = $this->request('secret');
			$model->jituan_hotelid = $this->request('jituan_hotelid');
			$model->yewu_hotelid = $this->request('yewu_hotelid');
			$model->cmmcod = $this->request('cmmcod');
			$model->restype = $this->request('restype');
			$model->pay_code = $this->request('pay_code');
			$model->empno = $this->request('empno');

			if ($model->save()) {
				return Tool::showRes([0, '操作成功']);
			}
			return Tool::showRes([400, '操作失败']);
		} catch (Exception $e) {
			echo $e->getMessage();
			// die(); // 终止异常
		}
	}

	//根据appid获取西软配置数据
	public function actionGetXopConfigByAppid(){
		$appid = $this->request('appid');
		$xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
		return Tool::showRes([0, '西软配置数据'],$xop_config_data);
	}
}
